﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ccit.Utils;
using Ccit.ClassLibrary;

namespace Ccit.SysPlat.Organ
{
    /// <summary>
    /// Description: 系统平台组织机构 -> 部门管理业务层
    /// Author: tim 2010-02-05
    /// </summary>
    public class DeptBLL
    {
        #region initialize 

        IDept _deptProcessor = null;

        public DeptBLL()
        {
            _deptProcessor = Factory.CreateObject(MsDept.assemblyName, MsDept.className, ImportComponentType.DotNetDll, null) as IDept;
        }

        public void Release()
        {
            _deptProcessor = null;
        }
        #endregion

        #region public methods
        /// <summary>
        /// 获取机构下的一级部门集合
        /// </summary>
        /// <param name="organId"></param>
        /// <returns></returns>
        public List<MsDept> GetStairDeptList(string organId)
        {
            if (string.IsNullOrEmpty(organId)) return new List<MsDept>();

            List<MsDept> lstDept = _deptProcessor.GetStairDeptList(organId);
            return lstDept;
        }


        /// <summary>
        /// 获取部门的下一级子部门集合
        /// </summary>
        /// <param name="deptId">部门Id</param>
        /// <param name="organId">机构Id</param>
        /// <returns></returns>
        public List<MsDept> GetSubDeptList(string deptId,string organId)
        {
            if (string.IsNullOrEmpty(deptId) || string.IsNullOrEmpty(organId)) return new List<MsDept>();

            List<MsDept> lstDept = _deptProcessor.GetSubDeptList(deptId,organId);
            return lstDept;
        }


        /// <summary>
        /// 部门删除
        /// </summary>
        /// <param name="deptId">部门Id</param>
        /// <returns></returns>
        public List<string> Delete(string deptId)
        {
            if (string.IsNullOrEmpty(deptId)) return new List<string> { "-999", "操作失败:缺少必要参数!" };
            List<string> lstResult = _deptProcessor.Delete(deptId);
            return lstResult;
        }


        /// <summary>
        /// 获取从当前部门开始的所有上级部门Id串(包含当前部门)
        /// </summary>
        /// <param name="deptId">当前部门Id</param>
        /// <returns></returns>
        public string GetDeptLevelStruct(string deptId)
        {
            if (string.IsNullOrEmpty(deptId)) return string.Empty;

            string structIds = _deptProcessor.GetDeptLevelStruct(deptId);
            return structIds;
        }

        /// <summary>
        /// 获取部门信息(消息对象)
        /// </summary>
        /// <param name="deptId">部门Id</param>
        /// <param name="isContainExtend">是否包含扩展属性</param>
        /// <returns></returns>
        public MsDept GetDeptInfo(string deptId, eTrueFalse isContainExtend)
        {
            if (string.IsNullOrEmpty(deptId)) return null;

            MsDept msDept = _deptProcessor.GetDeptInfo(deptId, isContainExtend);
            return msDept;
        }

        /// <summary>
        /// 按部门层级结构的形式获取机构下的所有有效的部门信息
        /// </summary>
        /// <param name="organId">机构Id</param>
        /// <param name="filterDeptId">过滤掉的部门Id(同时过滤掉部门下的所有子部门)</param>
        /// <returns></returns>
        public List<MsKeyValue> GetALLDepts(string organId, string filterDeptId)
        {
            if (string.IsNullOrEmpty(organId)) return new List<MsKeyValue>();

            List<MsKeyValue> lstDept = _deptProcessor.GetALLDepts(organId, filterDeptId);
            return lstDept;
        }

        /// <summary>
        /// 校验本机构下的部门是否同名
        /// </summary>
        /// <param name="deptId">部门Id</param>
        /// <param name="deptName">部门名称</param>
        /// <param name="organId">机构Id</param>
        /// <returns></returns>
        public bool IsExistSameName(string deptId, string deptName, string organId)
        {
            bool bExist = _deptProcessor.IsExistSameName(deptId, deptName, organId);
            return bExist;
        }

        /// <summary>
        /// 保存部门信息
        /// </summary>
        /// <param name="msDept">部门消息对象</param>        
        /// <returns>
        ///     【0】: 部门Id / 负数 (操作失败)
        ///     【1】: 操作提示消息
        ///     【2】:  是否有下级子节点
        ///     【3】:  显示顺序
        /// </returns>
        public List<string> Save(MsDept msDept)
        {
            if (null == msDept) return new List<string> { "-999", "操作失败:缺少必要参数!" };

            List<string> lstResult = _deptProcessor.Save(msDept);
            return lstResult;
        }

        /// <summary>
        /// 获取机构下的部门记录名-值对儿字典集合
        /// </summary>
        /// <param name="organId">机构Id</param>
        /// <param name="filterDeptIds">过滤掉的部门记录Id串</param>        
        /// <returns></returns>
        public List<MsKeyValue> GetDeptListByOrgan(string organId, string filterDeptIds)
        {
            if (string.IsNullOrEmpty(organId)) return new List<MsKeyValue>();

            return _deptProcessor.GetDeptListByOrgan(organId, filterDeptIds, string.Empty);
        }
        #endregion
    }
}
